package com.eight.cloud.payment.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.eight.cloud.core.global.exception.CustomException;
import com.eight.cloud.core.global.result.ResultCode;
import com.eight.cloud.core.utils.format.Validate;
import com.eight.cloud.mybatisplus.service.BaseService;
import com.eight.cloud.payment.common.enums.PayChannelEnum;
import com.eight.cloud.payment.common.enums.TradeTypeEnum;
import com.eight.cloud.payment.entity.SubPayOrderEntity;
import com.eight.cloud.payment.mapper.SubPayOrderMapper;
import com.eight.cloud.payment.model.dto.SubPayOrderDto;
import com.eight.cloud.payment.model.vo.SubPayOrderVo;
import com.eight.cloud.payment.service.ISubPayOrderService;
import com.eight.cloud.redisson.utils.RedisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

import static com.eight.cloud.core.global.constant.NumConstant.*;

/**
* 子商户支付订单信息 服务实现类
*
* @ClassName: SubPayOrderServiceImpl
* @Author TXC
* @Date 2025-01-07 17:22
*/
@Service
public class SubPayOrderServiceImpl extends BaseService<SubPayOrderMapper, SubPayOrderEntity> implements ISubPayOrderService {
    @Resource
    private RedisUtils redisUtils;

    @Override
    public SubPayOrderVo addOrder(SubPayOrderDto subPayOrderDto) {
        Validate.checkField(ObjectUtil::isEmpty, subPayOrderDto.getMerchantId(), "商户号");
        Validate.checkField(ObjectUtil::isEmpty, subPayOrderDto.getSubOrderId(), "订单号");
        Validate.checkField(ObjectUtil::isEmpty, subPayOrderDto.getAppid(), "应用id");
        Validate.checkField(ObjectUtil::isEmpty, subPayOrderDto.getTotal(), "订单金额");
        Validate.checkField(ObjectUtil::isEmpty, subPayOrderDto.getTradeType(), "支付方式");
        Validate.checkField(ObjectUtil::isEmpty, subPayOrderDto.getPayChannel(), "支付类型");
        if (subPayOrderDto.getTotal() <= ZERO) {
            throw new CustomException(500, "订单金额不能小于零");
        }
        SubPayOrderVo subPayOrder = findSubPayOrder(subPayOrderDto.getMerchantId(), subPayOrderDto.getSubOrderId(), subPayOrderDto.getAppid());
        if (ObjectUtil.isNotNull(subPayOrder)) {
            if (!Objects.equals(subPayOrder.getStatus(), SubPayOrderEntity.PayStatusEnum.UNPAID.getValue())) {
                throw new RuntimeException("订单已支付");
            } else {
                if (!Objects.equals(subPayOrderDto.getTotal(), subPayOrder.getTotal())) {
                    throw new RuntimeException("订单金额不一致");
                }
                return subPayOrder;
            }
        }
        SubPayOrderEntity subPayOrderEntity = convertToEntity(subPayOrderDto);
        subPayOrderEntity.setUuid(redisUtils.generateSerial(EIGHT, TWO, "pay", "order", "P", subPayOrderDto.getAppid()));
        subPayOrderEntity.setStatus(SubPayOrderEntity.PayStatusEnum.UNPAID.getValue());
        baseMapper.insert(subPayOrderEntity);
        SubPayOrderVo subPayOrderVo = convertToObject(subPayOrderEntity, SubPayOrderVo.class);
        subPayOrderVo.setTradeType(subPayOrderDto.getTradeType());
        subPayOrderVo.setPayChannel(subPayOrderDto.getPayChannel());
        return subPayOrderVo;
    }

    @Override
    public SubPayOrderVo findSubPayOrder(String merchantId, String subOrderId, String appid) {
        SubPayOrderEntity order = lambdaQuery().eq(SubPayOrderEntity::getMerchantId, merchantId)
                .eq(SubPayOrderEntity::getSubOrderId, subOrderId)
                .eq(SubPayOrderEntity::getAppid, appid).one();
        SubPayOrderVo subPayOrderVo = convertToObject(order, SubPayOrderVo.class);
        subPayOrderVo.setTradeType(TradeTypeEnum.getByCode(order.getTradeType()));
        subPayOrderVo.setPayChannel(PayChannelEnum.getByCode(order.getPayChannel()));
        return subPayOrderVo;
    }

    @Override
    public boolean updateSubPayOrder(String uuid, String transactionId, Integer status) {
        if (StrUtil.isNotBlank(uuid)) {
            return baseMapper.update(lambdaUpdateWrapper()
                    .set(StrUtil.isNotBlank(transactionId), SubPayOrderEntity::getTransactionId, transactionId)
                    .set(ObjectUtil.isNotNull(status), SubPayOrderEntity::getStatus, status)
                    .eq(SubPayOrderEntity::getUuid, uuid)) > ZERO;
        }
        throw new CustomException(ResultCode.PARAM_IS_BLANK);
    }

    @Override
    public SubPayOrderVo findSubPayOrderByTransactionId(String merchantId, String transactionId, String appid) {
        SubPayOrderEntity order = lambdaQuery().eq(SubPayOrderEntity::getMerchantId, merchantId)
                .eq(SubPayOrderEntity::getTransactionId, transactionId)
                .eq(SubPayOrderEntity::getAppid, appid).one();
        return convertToObject(order, SubPayOrderVo.class);
    }
}
